import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:freemusic/page/home/HomePage.dart';

void main() => runApp(FreeMusicApp());

class FreeMusicApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return FreeMusicState();
  }
}

class FreeMusicState extends State<FreeMusicApp> {
  DateTime lastPopTime;

  Future<bool> _handleOnWillPop() async {
    //点击返回键的操作
    if (lastPopTime == null ||
        DateTime.now().difference(lastPopTime) > Duration(seconds: 2)) {
      lastPopTime = DateTime.now();
      Fluttertoast.showToast(msg: '再按一次退出');
      return false;
    } else {
      lastPopTime = DateTime.now();
      // 退出app
      SystemNavigator.pop();
      return true;
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'FreeMusic',
      home: WillPopScope(
        onWillPop: this._handleOnWillPop,
        child: HomePage(),
      ),
    );
  }
}
